#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int totalFruit(vector<int>& f)
    {
        int n = f.size();
        int hash[100001] = { 0 };
        int len = 0, kinds = 0;
        for (int left = 0, right = 0; right < n; right++)
        {

            if (hash[f[right]] == 0) kinds++;

            hash[f[right]]++;
            while (kinds > 2)
            {
                hash[f[left]]--;
                if (hash[f[left]] == 0) kinds--;
                left++;
            }
            len = max(len, right - left + 1);
        }
        if (kinds == 1) return n;
        else return len;
    }
};